<?php
include '../../../wp-load.php';
get_currentuserinfo();
global $user_level;
if($user_level == 10) {

}
else{
	$url = get_option('siteurl') . '/wp-login.php?action=logout';
	header("Location: $url");exit();
}
?>
<?php

if($_GET[changeStatus] == '1'){
	change_order_level($_GET[oid],$_GET[new_level]);
	
		if($_GET[new_level] == '6'){
			$table 	= is_dbtable_there('orders');
			$qStr 	= "SELECT * FROM $table WHERE oid = '$_GET[oid]' LIMIT 1";
			$res 	= mysql_query($qStr);
			$order 	= mysql_fetch_assoc($res);
		
									// we send customer a notification email about the shipping					
									$to 					= $order[email];
									$subject 				= 'Your Order '.$order[oid].' has been shipped';
						
									$filename				= 'email/'.get_option('wps_shipping_email');
									$message				= file_get_contents($filename);
									$admin_email_address	= get_option('wps_paypal_email');
									$domain					= get_option('home');
									$domain 				= substr($domain, 7); 									

									$em_logo_path 	= get_option('home') .'/wp-content/themes/wpShop/images/logo/' . get_option('wps_email_logo');	
									$message		= str_replace('[##Email-Logo##]', $em_logo_path, "$message");									
									$message		= str_replace('[##biz##]', get_option('wps_shop_name'), "$message");
									$message		= str_replace('[##name##]', $order[f_name].' '.$order[l_name] , "$message");					
									$message		= str_replace('[##orderid##]', $order[oid] , "$message");	

											
									html_mail($to,$subject,$message,$admin_email_address,$domain);	
		$url2 = get_option('siteurl') . '/wp-content/themes/wpShop/shop-backend.php';							
		header("Location: $url2");exit();
		}
	
}

					// get orders from db
					$odata[1]	= array();
					$odata[2]	= array();
					$odata[3]	= array();
					$odata[4]	= array();
					$odata[5]	= array();
					$counter1	= 0;
					$counter2	= 0;
					$counter3	= 0;
					$counter4	= 0;
					$counter5	= 0;
					$table 	= is_dbtable_there('orders');
					$qStr 	= "SELECT * FROM $table ORDER BY order_time DESC";
					$res 	= mysql_query($qStr);

					while($order 	= mysql_fetch_assoc($res)){				
										
						switch($order[level]){
							case '4':					
								$details 				= show_orders($order[who]);							
								$order[items]			= $details;
								$odata[1][$counter1]	= $order;								
								$counter1++;
							break; 
							
							case '5':
								$details 				= show_orders($order[who]);							
								$order[items]			= $details;
								$odata[2][$counter2]	= $order;								
								$counter2++;
							break; 
							
							case '6':
								$details 				= show_orders($order[who]);							
								$order[items]			= $details;
								$odata[3][$counter3]	= $order;								
								$counter3++;								
							break; 
							
							case '7':
								$details 				= show_orders($order[who]);							
								$order[items]			= $details;
								$odata[4][$counter4]	= $order;								
								$counter4++;
							break; 
							
							case '8':
								$details 				= show_orders($order[who]);							
								$order[items]			= $details;
								$odata[5][$counter5]	= $order;								
								$counter5++;
							break; 
						}
						
					}
					
	

					$table_header = "
						<table border='1' class='widefat' >
							<thead>
								<tr>
									<th>Order-No:</th>
									<th>Date</th>
									<th>Who?</th>
									<th>Address</th>
									<th>Amount</th>
									<th>Details</th>
									<th>Invoice</th>
									<th>PayPal</th>
									<th>Status</th>
								</tr>
							</thead>
							<tbody>
						";	
						
					$table_footer 	= "</tbody></table><br/><br/><br/>";				
					$empty_message 	= "No orders with this status.<br/><br/><br/>";
					$date_format	= "j.m.Y - G:i:s";
								
					
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
		<html xmlns='http://www.w3.org/1999/xhtml' <?php language_attributes(); ?>>

	<head profile='http://gmpg.org/xfn/11'>	
		<link media="all" type="text/css" href="<?php bloginfo('template_url'); ?>/css/shop_admin.css" rel="stylesheet"></link>
		
		
	<title>eGoods :: Shop-Backend :: Manage Orders</title>
	</head>
	<body class="wp-admin">
	<div id="wpwrap">
	<div id="wpcontent">
	<div id="wphead">
		<h1>eGoods :: Manage orders
			<span id="viewsite"><a href='<?php bloginfo('url') ?>/wp-admin/themes.php?page=functions.php'>back to Wordpress-Admin</a></span>
		</h1>
	</div>
	<div id="wpbody">
		<div class="wrap">
	<?php	
	
	if((count($odata[5])) > 0){
	echo "<h4>Status 1: Newly received &amp; but paypal payment yet pending</h4>";
	
		echo $table_header; 
		
				foreach($odata[5] as $k => $order){
						
					$date = date($date_format,$order[order_time]);
					
					echo "<tr style='background-color: coral;'>
					<td>$order[oid]</td>
					<td>$date</td>
					<td>$order[l_name] $order[f_name]<br/><a href='mailto:$order[email]'>Send email</a></td>
					<td>";
					if($order[invoice] == '1'){
						echo "$order[street]<br/>$order[town], $order[state] $order[zip]<br/>$order[country]";
					}
					else { echo "&nbsp;"; }
					echo "</td>
					<td>$order[amount] "; echo get_option('wps_currency_code'); echo  "</td>
					
					<td>"; 					
					echo list_order_items($order[items]);				
					echo "</td>
					<td><a href='pdf/bills/bill_{$order[tracking_id]}.pdf' target='_blank'>bill</a></td>
					<td><a href='https://www.paypal.com/de/vst/id={$order[txn_id]}' target='_blank' title='{$order[txn_id]}'>Check txn_id</a></td>
					<td>
					<a href='?changeStatus=1&oid=$order[oid]&new_level=4'>
					<b>Change to: Ok-payment has been cleared.</b>
					</a>
					</td>
					</tr>";
				}
				
		echo $table_footer;
	}
	
	
	
	
	
	echo "<h4>Status 2: Newly received &amp; paid</h4>";

	if((count($odata[1])) > 0){
	
		echo $table_header; 
		
				foreach($odata[1] as $k => $order){
						
					$date = date($date_format,$order[order_time]);
					
					echo "<tr>
					<td>$order[oid]</td>
					<td>$date</td>
					<td>$order[l_name] $order[f_name]<br/><a href='mailto:$order[email]'>Send email</a></td>
					<td>";
					if($order[invoice] == '1'){
						echo "$order[street]<br/>$order[town], $order[state] $order[zip]<br/>$order[country]";
					}
					else { echo "&nbsp;"; }
					echo "</td>
					<td>$order[amount] "; echo get_option('wps_currency_code'); echo  "</td>
					
					<td>"; 					
					echo list_order_items($order[items]);				
					echo "</td>
					<td><a href='pdf/bills/bill_{$order[tracking_id]}.pdf' target='_blank'>bill</a></td>
					<td><a href='https://www.paypal.com/de/vst/id={$order[txn_id]}' target='_blank' title='{$order[txn_id]}'>Check txn_id</a></td>
					<td>
					<a href='?changeStatus=1&oid=$order[oid]&new_level=8'>
					Change to: Payment pending
					</a>
					</td>
					</tr>";
				}
				
		echo $table_footer;
	}
	else {
		echo $empty_message;
	}
	
	/*
	echo "<h4>Status 2: In process - being packed.</h4>";
	
	
		if((count($odata[2])) > 0){
	
	echo $table_header; 
	
				foreach($odata[2] as $k => $order){
					
				$date = date($date_format,$order[order_time]);
				
				echo "<tr>
				<td>$order[oid]</td>
				<td>$date</td>
				<td>$order[l_name] $order[f_name]<br/><a href='mailto:$order[email]'>Send email</a></td>
				<td>$order[street]<br/>$order[town], $order[state] $order[zip]<br>$order[country]</td>
				<td>$order[amount] "; echo get_option('wps_currency_code'); echo  "</td>
				
				<td>"; 					
					echo list_order_items($order[items]);				
					echo "</td>
				<td><a href='pdf/bills/bill_{$order[tracking_id]}.pdf' target='_blank'>bill</a></td>
				<td><a href='https://www.paypal.com/de/vst/id={$order[txn_id]}' target='_blank' title='{$order[txn_id]}'>Check txn_id</a></td>
				<td>
				<a href='?changeStatus=1&oid=$order[oid]&new_level=4'>
				Change to: Newly received &amp; paid
				</a> 
				<br><a href='?changeStatus=1&oid=$order[oid]&new_level=6'>
				Change to: Ready - being shipped
				</a>
				</td>
				</tr>";
			}
	echo $table_footer;
		}
	else {
		echo $empty_message;
	}

		
	echo "<h4>Status 3: Ready - being shipped</h4>";
	
		if((count($odata[3])) > 0){
	
	echo $table_header; 
				foreach($odata[3] as $k => $order){
					
				$date = date($date_format,$order[order_time]);
				
				echo "<tr>
				<td>$order[oid]</td>
				<td>$date</td>
				<td>$order[l_name] $order[f_name]<br/><a href='mailto:$order[email]'>Send email</a></td>
				<td>$order[street]<br/>$order[town], $order[state] $order[zip]<br>$order[country]</td>
				<td>$order[amount] "; echo get_option('wps_currency_code'); echo  "</td>
				
				<td>"; 					
					echo list_order_items($order[items]);				
					echo "</td>
				<td><a href='pdf/bills/bill_{$order[tracking_id]}.pdf' target='_blank'>bill</a></td>
				<td><a href='https://www.paypal.com/de/vst/id={$order[txn_id]}' target='_blank' title='{$order[txn_id]}'>Check txn_id</a></td>
				<td>
				<a href='?changeStatus=1&oid=$order[oid]&new_level=5'>
				Change to: In process - being packed
				</a> 
				<br><a href='?changeStatus=1&oid=$order[oid]&new_level=7'>
				Change to: Completed - order archived for later reference
				</a>
				</td>
				</tr>";
			}
	echo $table_footer;
		}
	else {
		echo $empty_message;
	}

	
			
	echo "<h4>Status 4: Completed - order archived for later reference</h4>";
	
		if((count($odata[4])) > 0){
		
		
	echo $table_header; 
				foreach($odata[4] as $k => $order){
					
				$date = date($date_format,$order[order_time]);
				
				echo "<tr>
				<td>$order[oid]</td>
				<td>$date</td>
				<td>$order[l_name] $order[f_name]<br/><a href='mailto:$order[email]'>Send email</a></td>
				<td>$order[street]<br/>$order[town], $order[state] $order[zip]<br>$order[country]</td>
				<td>$order[amount] "; echo get_option('wps_currency_code'); echo  "</td>
				
				<td>"; 					
					echo list_order_items($order[items]);				
					echo "</td>
				<td><a href='pdf/bills/bill_{$order[tracking_id]}.pdf' target='_blank'>bill</a></td>
				<td><a href='https://www.paypal.com/de/vst/id={$order[txn_id]}' target='_blank' title='{$order[txn_id]}'>Check txn_id</a></td>
				<td>
				<a href='?changeStatus=1&oid=$order[oid]&new_level=6'>
				Change to: Ready - being shipped
				</a>
				</td>
				</tr>";
			}
	echo $table_footer;
		}
	else {
		echo $empty_message;
	}
*/
	
	echo "</div></div></div><div id='footer'></div></div></body></html>";
?>